﻿using System.Web.Mvc;
using Inovout.Services;
using Inovout.Models;
using System.Linq;
using System.Collections.Specialized;
using Newtonsoft.Json;

namespace Inovout.Web.Mvc
{
    public class RepositoryController<T> : BaseController where T : class
    {
        protected virtual IRepositoryService<T> RepositoryService
        {
            get;
            private set;
        }
        protected RepositoryController()
        {
            RepositoryService = ComponentRegistry.Resolve<IRepositoryService<T>>();
        }

        public virtual ActionResult List(int? pageIndex)
        {
            if (pageIndex.HasValue)
            {
                return View(RepositoryService.FindAll(pageIndex.Value));
            }
            else
            {
                return View(RepositoryService.FindAll());
            }
        }
        public virtual ActionResult Create()
        {
            BeforeCreateOrEdit(true);
            return View();
        }
        public virtual ActionResult Edit(int id)
        {
            BeforeCreateOrEdit(false);
            return View(RepositoryService.FindById(id));
        }
        protected virtual void BeforeCreateOrEdit(bool isCreate)
        {

        }
        [HttpPost]
        public virtual ActionResult Save(T entitry)
        {
            RepositoryService.Save(entitry);
            if (base.Request.IsAjaxRequest())
            {
                return Json(WrapJsonObect(entitry));
            }
            else
            {
                return RedirectToAction("List");
            }
        }
        protected virtual object WrapJsonObect(T entitry)
        {
            return entitry;
        }
        [AllowAnonymous]
        public ActionResult ValidateUnique()
        {
            DataObjectTypeDescriptor dataObjectTypeDescriptor = DataObjectTypeDescriptor.Get<T>();
            object id = null;
            NameValueCollection propertyDictionary = base.HttpContext.Request.HttpMethod.ToLower().Equals("get")
                ? base.HttpContext.Request.QueryString
                : base.HttpContext.Request.Form;
            if (propertyDictionary.AllKeys.Contains(dataObjectTypeDescriptor.IdentifierProperty.Name))
            {
                id = dataObjectTypeDescriptor.IdentifierProperty.ConvertValueFrom(propertyDictionary[dataObjectTypeDescriptor.IdentifierProperty.Name]);
            }
            //只会剩下一个。
            DataObjectPropertyDescriptor dataObjectPropertyDescriptor = dataObjectTypeDescriptor.Properties.Single(
                    p => propertyDictionary.AllKeys.Contains(p.Name) && !p.Name.Equals(dataObjectTypeDescriptor.IdentifierProperty.Name)
                ) as DataObjectPropertyDescriptor;
            if (RepositoryService.IsUnique(id, dataObjectPropertyDescriptor.Name, dataObjectPropertyDescriptor.ConvertValueFrom(propertyDictionary[dataObjectPropertyDescriptor.Name])))
            {
                return Json(true, JsonRequestBehavior.AllowGet);
            }
            else
            {
                UniqueAttribute uniqueAttribute = dataObjectPropertyDescriptor.GetAttribute<UniqueAttribute>();
                return Json(uniqueAttribute.ErrorMessage, JsonRequestBehavior.AllowGet);
            }
        }
    }
}
